<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Service extends MY_Controller {

    public function index() {
        redirect("home/error");
    }

    public function imageUpload($type, $makeThumb = false, $thumbWidth = 1, $thumbHeight = 1) {
        $isSuccess = false;
        $imgSrc = "";
        $message = "";
        $file = $_FILES["file"];
        $ext = strtolower(end(explode(".", $file["name"])));
        $tempName = "" . time() . "_" . rand(1, 10000);
        $fileConf = Array(
            "allowed_types" => "gif|jpg|png|jpeg",
            "max_size" => 0,
            "upload_path" => APPPATH . "temp/img/",
            "file_name" => $tempName . "." . $ext,
            "overwrite" => true
        );

        $this->load->library("upload", $fileConf);
        if (!$this->upload->do_upload("file")) {
            $message = $this->upload->display_errors();
            $isSuccess = false;
        } else {
            tempFolderCleanUp("img");
            $data = $this->upload->data();
            // image resizing
            $imgConf = Array(
                "source_image" => $data["full_path"],
                "maintain_ratio" => false,
                "width" => $data["image_width"],
                "height" => $data["image_height"]
            );
            switch ($type) {
                case "thumb" :
                    $imgConf["width"] = 100;
                    $imgConf["height"] = 100;
                    break;
                case "banner" :
                    $imgConf["width"] = 575;
                    $imgConf["height"] = 225;
                    break;
                case "userpic" :
                    $imgConf["width"] = 120;
                    $imgConf["height"] = 140;
                    break;
                case "kegpic" :
                    $imgConf["width"] = 400;
                    $imgConf["height"] = 400;
                    $imgConf["maintain_ratio"] = TRUE;
                    break;
                case "gal" :
                    $imgConf["width"] = 600;
                    $imgConf["height"] = 600;
                    $imgConf["maintain_ratio"] = TRUE;
                    break;
            }
            $this->load->library("image_lib", $imgConf);
            $this->image_lib->resize();
            // generate thumbnail
            if ($makeThumb) {
                copy($data["full_path"], $data["full_path"] . "tmb");
                $this->image_lib->initialize(Array(
                    "source_image" => $data["full_path"] . "tmb",
                    "maintain_ratio" => false,
                    "width" => $thumbWidth,
                    "height" => $thumbHeight
                ));
                $this->image_lib->resize();
            }
            //---
            $isSuccess = true;
            $imgSrc = temp_url("img/" . $data["file_name"]);
        }

        $result = " {
                ";
        $result .= "\"success\" : " . ($isSuccess ? "true" : "false") . ",";
        $result .= "\"src\" : \"" . $imgSrc . "\",";
        $result .= "\"tempname\" : \"" . ($isSuccess ? $fileConf["file_name"] : "") . "\",";
        $result .= "\"message\" : \"" . $message . "\"";
        $result .= "}";

        echo $result;
        return;
    }

    public function autoCompleteSearch($type) {
        $keywords = $this->input->post("keywords");
        $result = Array();

        switch ($type) {
            case "kegbyjudul" :
                $this->load->database("homepage");
                $this->db->select('activity.id, content.title')->from('activity, content')->where("activity.is_approved", true)->where("activity.is_deleted", false)->where("activity.content_id = content.id")->like('content.title', $keywords, 'both');
                $query = $query = $this->db->get();
                foreach ($query->result() as $row) {
                    $resrow = new stdClass();
                    $resrow->label = $row->title;
                    $resrow->value = $row->id;
                    array_push($result, $resrow);
                }
                break;
            case "locall" :
                $this->load->database("homepage");
                // region
                $this->db->select('region.code, region.display_name')->from('region')->like('region.display_name', $keywords, 'both')->or_like('region.code', $keywords, 'after');
                $queryReg = $this->db->get();
                foreach ($queryReg->result() as $row) {
                    $resrow = new stdClass();
                    $resrow->label = "Region " . $row->code . " : " . $row->display_name;
                    $resrow->value = $row->code;
                    array_push($result, $resrow);
                }
                // prov
                $this->db->select('provinsi.code, provinsi.display_name')->from('provinsi')->like('provinsi.display_name', $keywords, 'both')->or_like('provinsi.code', $keywords, 'after');
                $queryProv = $this->db->get();
                foreach ($queryProv->result() as $row) {
                    $resrow = new stdClass();
                    $resrow->label = "Provinsi " . $row->display_name;
                    $resrow->value = $row->code;
                    array_push($result, $resrow);
                }
                // kab
                $this->db->select('kabupaten.code, kabupaten.display_name, provinsi.display_name as prov_name')->from('kabupaten, provinsi');
                $this->db->where('provinsi.code = kabupaten.provinsi_code');
                $this->db->like('kabupaten.display_name', $keywords, 'both')->or_like('kabupaten.code', $keywords, 'after');
                $queryKab = $this->db->get();
                foreach ($queryKab->result() as $row) {
                    $resrow = new stdClass();
                    $resrow->label = "Kab. " . $row->display_name . ", " . $row->prov_name;
                    $resrow->value = $row->code;
                    array_push($result, $resrow);
                }

                // kec
                $this->db->select('kecamatan.code, kecamatan.display_name, kabupaten.display_name as kab_name, provinsi.display_name as prov_name')->from('kecamatan, kabupaten, provinsi');
                $this->db->where('kabupaten.code = kecamatan.kabupaten_code')->where('provinsi.code = kabupaten.provinsi_code');
                $this->db->like('kecamatan.display_name', $keywords, 'both')->or_like('kecamatan.code', $keywords, 'after');
                $queryKec = $this->db->get();
                foreach ($queryKec->result() as $row) {
                    $resrow = new stdClass();
                    $resrow->label = "Kec. " . $row->display_name . ", " . $row->kab_name . ", " . $row->prov_name;
                    $resrow->value = $row->code;
                    array_push($result, $resrow);
                }
                // desa
                /*
                  $this->db->select('perdesaan.code, perdesaan.display_name, kecamatan.display_name as kec_name, kabupaten.display_name as kab_name, provinsi.display_name as prov_name')->from('perdesaan, kecamatan, kabupaten, provinsi');
                  $this->db->where('kecamatan.code = perdesaan.kecamatan_code')->where('kabupaten.code = kecamatan.kabupaten_code')->where('provinsi.code = kabupaten.provinsi_code');
                  $this->db->like('perdesaan.display_name', $keywords, 'after')->or_like('perdesaan.code', $keywords, 'after');
                  $queryDes = $this->db->get();
                  foreach ($queryDes->result() as $row) {
                  $resrow = new stdClass();
                  $resrow->label = "Desa " . $row->display_name . ", Kec. " . $row->kec_name . ", " . $row->kab_name . ", " . $row->prov_name;
                  $resrow->value = $row->code;
                  array_push($result, $resrow);
                  }
                 */
                break;
            case "lockab" :
                $this->load->database("homepage");
                $this->db->select('kabupaten.code, kabupaten.display_name, provinsi.display_name as prov_name')->from('kabupaten, provinsi');
                $this->db->where('provinsi.code = kabupaten.provinsi_code');
                $this->db->like('kabupaten.display_name', $keywords, 'both')->or_like('kabupaten.code', $keywords, 'after');
                $queryKab = $this->db->get();
                foreach ($queryKab->result() as $row) {
                    $resrow = new stdClass();
                    $resrow->label = "Kab. " . $row->display_name . ", " . $row->prov_name;
                    $resrow->value = $row->code;
                    array_push($result, $resrow);
                }
                break;
        }
        echo json_encode($result);
    }

    public function getDataJSON($type) {
        $result = NULL;
        switch ($type) {
            case "locdet" :
                $code = intval($this->input->post("kode"), 10);
                if ($code < 1) {
                    // INDONESIA
                    $this->load->model("Region_model");
                    $result = new stdClass();
                    $result->display_name = "INDONESIA";
                    $result->summary = "";
                    $result->longlatitude = 120;
                    $result->latitude = -2.5;
                    $result->title = $result->display_name;
                    $result->level = "IND";
                    //$result->regions = $this->Region_model->findAll();
                } else if ($code < 10) {
                    // REGION
                    $this->load->model("Region_model");
                    $this->load->model("Provinsi_model");
                    $result = $this->Region_model->findById($code);
                    $result->title = "Region " . $result->code . " : " . $result->display_name;
                    $result->level = "REG";
                    //$result->provinsis = $this->Provinsi_model->findAll(Array("region_code" => $result->code));
                } else if ($code < 100) {
                    // PROVINSI
                    $this->load->model("Provinsi_model");
                    $this->load->model("Kabupaten_model");
                    $result = $this->Provinsi_model->findById($code);
                    $result->title = "Prov. " . $result->display_name;
                    $result->level = "PROV";
                    //$result->kabupatens = $this->Kabupaten_model->findAll(Array("provinsi_code" => $result->code));
                } else if ($code < 10000) {
                    // KABUPATEN
                    $this->load->model("Kabupaten_model");
                    $this->load->model("Kecamatan_model");
                    $result = $this->Kabupaten_model->findById($code);
                    $result->title = "Kab. " . $result->display_name;
                    $result->level = "KAB";
                    //$result->kecamatans = $this->Kecamatan_model->findAll(Array("kabupaten_code" => $result->code));
                } else if ($code < 10000000) {
                    // KECAMATAN
                    $this->load->model("Kecamatan_model");
                    $this->load->model("Perdesaan_model");
                    $result = $this->Kecamatan_model->findById($code);
                    $result->title = "Kec. " . $result->display_name;
                    $result->level = "KEC";
                    //$result->desas = $this->Perdesaan_model->findAll(Array("kecamatan_code" => $result->code));
                } else if ($code < 10000000000) {
                    // PERDESAAN
                    $this->load->model("Perdesaan_model");
                    $result = $this->Perdesaan_model->findById($code);
                    $result->title = "Desa " . $result->display_name;
                    $result->level = "DESA";
                }
                $this->load->model("De_location_model");
                if ($result->level != "REG") {
                    $result->locarr = $this->De_location_model->findAll("`loc_ref` LIKE '" . $code . "%'");
                }
                break;
            case "kegbyyear" :
                $year = $this->input->post("year");
                $this->load->model("Kegiatan_model");
                $result = $this->Kegiatan_model->findAll("start_date LIKE '" . $year . "%' OR end_date LIKE '" . $year . "%'", true);
                break;
            case "kegbyid" :
                $id = $this->input->post("id");
                $this->load->model("Kegiatan_model");
                $result = $this->Kegiatan_model->findById($id, true, 2);
                break;
            case "bkbyid" :
                $id = $this->input->post("id");
                $this->load->model("Beritakegiatan_model");
                $result = $this->Beritakegiatan_model->findById($id, true, 3);
                break;
            case "bkbykeg" :
                $kegId = $this->input->post("kegid");
                $aa = $this->input->post("aa");
                $page = ($this->input->post("page") == NULL ? -1 : $this->input->post("page"));
                $limit = ($this->input->post("limit") == NULL ? -1 : $this->input->post("limit"));
                $this->load->model("Kegiatan_model");
                $this->load->model("Beritakegiatan_model");
                $keg = $this->Kegiatan_model->findById($kegId, false);
                if ($keg != NULL) {
                    if ($aa) {
                        $authorized = Array("kegiatan_auditor");
                        if ($this->isAuth($authorized)) {
                            $result = $this->Beritakegiatan_model->findAll(Array("activity_id" => $kegId), true, 1, $page, $limit);
                        }
                    } else {
                        $result = $this->Beritakegiatan_model->findAll(Array("activity_id" => $kegId, "is_approved" => true), true, 1, $page, $limit);
                    }
                }
                break;
            case "bkrecents" :
                $this->load->model("Beritakegiatan_model");
                $page = $this->input->post("page");
                $beritaRecents = $this->Beritakegiatan_model->fetchRecent($page);
                $result = $beritaRecents;
                break;
            case "bkpopulars" :
                $this->load->model("Beritakegiatan_model");
                $page = $this->input->post("page");
                $beritaRecents = $this->Beritakegiatan_model->fetchRecent($page);
                $result = $beritaRecents;
                break;
            case "brtbyid" :
                $id = $this->input->post("id");
                $this->load->model("Artikel_model");
                $result = $this->Artikel_model->beritaById($id, true, 2);
                break;
            case "krbyid" :
                $id = $this->input->post("id");
                $this->load->model("Artikel_model");
                $result = $this->Artikel_model->krById($id, true, 2);
                break;
            case "pjbyid" :
                $id = $this->input->post("id");
                $this->load->model("Artikel_model");
                $result = $this->Artikel_model->pjById($id, true, 2);
                break;
            case "artrecents" :
                $this->load->model("Artikel_model");
                $page = $this->input->post("page");
                $type = $this->input->post("type");
                switch (strtoupper($type)) {
                    case Artikel_model::TYPE_BERITA:
                        if ($page > 0) {
                            $result = $this->Artikel_model->beritaRecent((2 * $page) / 3, 6);
                        } else {
                            $result = $this->Artikel_model->beritaRecent($page, 4);
                        }
                        break;
                    case Artikel_model::TYPE_KABAR_RELAWAN:
                        $result = $this->Artikel_model->krRecent($page, 6);
                        break;
                    case Artikel_model::TYPE_PUBLIK:
                        $result = $this->Artikel_model->pjRecent($page);
                        break;
                }
                break;
            case "artpopulars" :
                $this->load->model("Artikel_model");
                $page = $this->input->post("page");
                $type = $this->input->post("type");
                switch (strtoupper($type)) {
                    case Artikel_model::TYPE_BERITA:
                        $result = $this->Artikel_model->beritaRecent($page);
                        break;
                    case Artikel_model::TYPE_KABAR_RELAWAN:
                        $result = $this->Artikel_model->krRecent($page);
                        break;
                    case Artikel_model::TYPE_PUBLIK:
                        $result = $this->Artikel_model->pjRecent($page);
                        break;
                }
                break;
            case "banpslist" :
                $this->load->database("data_ps");
                $banps = $this->input->post("banps");
                $level = $this->input->post("level");
                switch ($level) {
                    case "kab" :
                        $this->db->select("kabupaten.*, banps.name as name");
                        $this->db->from("kabupaten, kabupaten_banps, banps");
                        $this->db->where("kabupaten_banps.banps_id = banps.id");
                        $this->db->where("kabupaten_banps.kabupaten_code = kabupaten.code");
                        $this->db->where("kabupaten_banps.banps_id", $banps);
                        $query = $this->db->get();
                        $result = $query->result();
                        break;
                }
                break;
            case "bancatyear" :
                $this->load->database("data_ps");
                $year = $this->input->post("year");
                $this->db->from("banps");
                $this->db->where("year", $year);
                $query = $this->db->get();
                $result = $query->result();
                break;
            case "galpop" :
                $this->load->model("Gallery_model");
                $perpage = 5;
                if ($this->input->post("perpage")) {
                    $perpage = $this->input->post("perpage");
                }
                $result = $this->Gallery_model->fetchHot($this->input->post("page"), $perpage);
                break;
            case "galbar" :
                $this->load->model("Gallery_model");
                $perpage = 5;
                if ($this->input->post("perpage")) {
                    $perpage = $this->input->post("perpage");
                }
                $result = $this->Gallery_model->fetchRecent($this->input->post("page"), $perpage);
                break;
            case "galhl" :
                $this->load->model("Gallery_model");
                $perpage = 5;
                if ($this->input->post("perpage")) {
                    $perpage = $this->input->post("perpage");
                }
                $result = $this->Gallery_model->fetchHL($this->input->post("page"), $perpage);
                break;
            case "provbyreg" :
                $this->load->model("Provinsi_model");
                $result = $this->Provinsi_model->findAll(Array("region_code" => $this->input->post("reg_code")));
                break;
            case "kabbyprov" :
                $this->load->model("Kabupaten_model");
                $result = $this->Kabupaten_model->findAll(Array("provinsi_code" => $this->input->post("prov_code")));
                break;
            case "kecbykab" :
                $this->load->model("Kecamatan_model");
                $result = $this->Kecamatan_model->findAll(Array("kabupaten_code" => $this->input->post("kab_code")));
                break;
            case "desabykec" :
                $this->load->model("Perdesaan_model");
                $result = $this->Perdesaan_model->findAll(Array("kecamatan_code" => $this->input->post("kec_code")));
                break;
        }
        echo json_encode($result);
    }

}

